/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.entity.MappingService;
import han.bifang.gwt.client.common.entity.ServiceQos;
import han.bifang.gwt.client.service.ServiceConstants;
import han.bifang.gwt.client.service.ServiceContentConstants;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class CreateServiceConfigContent extends Content {

	// private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private ServiceMessages cons;

	private VerticalPanel panel = null;

	private ListBox log, acl, retry;
	
	
	public CreateServiceConfigContent(ServiceMessages cons, GwtContext context) {
		this.cons = cons;
		this.context = context;
		panel = new VerticalPanel();
		panel.setSpacing(5);
		panel.setWidth("100%");

		initWidget(panel);

	}

	public void onLoad() {
		HTML h = new HTML(cons.configService());
		panel.add(h);

		Grid table = new Grid(3, 2);
		table.setCellPadding(1);
		table.setCellSpacing(1);
		table.setBorderWidth(1);

		ColumnFormatter cf = table.getColumnFormatter();
		cf.setWidth(0, "15%");
		cf.setWidth(1, "45%");

		CellFormatter ff = table.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		ff.setStyleName(1, 0, "tablecol");
		ff.setStyleName(2, 0, "tablecol");

		table.setHTML(0, 0, cons.log());
		table.setHTML(1, 0, cons.acl());
		table.setHTML(2, 0, cons.retry());

		log = new ListBox();
		log.addItem(cons.no(), EntityConstants.QOS_NO);
		log.addItem(cons.yes(), EntityConstants.QOS_YES);

		acl = new ListBox();
		acl.addItem(cons.no(), EntityConstants.QOS_NO);
		acl.addItem(cons.yes(), EntityConstants.QOS_YES);

		retry = new ListBox();
		retry.addItem(cons.no(), EntityConstants.QOS_NO);
		retry.addItem(cons.yes(), EntityConstants.QOS_YES);

		if (context.get(ServiceConstants.SERVICE_QOS) == null) {
			log.setSelectedIndex(0);
			acl.setSelectedIndex(0);
			retry.setSelectedIndex(0);
		} else {
			ArrayList ql = (ArrayList) context
					.get(ServiceConstants.SERVICE_QOS);
			ServiceQos q = (ServiceQos) ql.get(0);
			if (q.getValue().equals(EntityConstants.QOS_YES)) {
				log.setSelectedIndex(1);
			} else {
				log.setSelectedIndex(0);
			}
			q = (ServiceQos) ql.get(1);
			if (q.getValue().equals(EntityConstants.QOS_YES)) {
				acl.setSelectedIndex(1);
			} else {
				acl.setSelectedIndex(0);
			}
			q = (ServiceQos) ql.get(2);
			if (q.getValue().equals(EntityConstants.QOS_YES)) {
				retry.setSelectedIndex(1);
			} else {
				retry.setSelectedIndex(0);
			}

		}

		table.setWidget(0, 1, log);
		table.setWidget(1, 1, acl);
		table.setWidget(2, 1, retry);

		HorizontalPanel hp = new HorizontalPanel();

		Button p = new Button(cons.previous());
		p.addClickListener(new ClickListener() {
			public void onClick(Widget send) {
				MappingService s = (MappingService)context.get(ServiceConstants.MAPPING_SERVICE);
				HashMap h = new HashMap();
				h.put("domainId", s.getDomainId());
				listener.onContentChanged(
						ServiceContentConstants.CREATE_SERVICE_CONFIG,
						ServiceContentConstants.CREATE_SERVICE_BASIC, h);

			}
		});

		Button b = new Button(cons.next());
		b.addClickListener(new ClickListener() {
			public void onClick(Widget send) {

				ArrayList qos = new ArrayList();
				ServiceQos q = new ServiceQos();
				q.setName(EntityConstants.QOS_LOG);
				q.setValue(log.getValue(log.getSelectedIndex()));

				qos.add(q);

				q = new ServiceQos();
				q.setName(EntityConstants.QOS_ACL);
				q.setValue(acl.getValue(acl.getSelectedIndex()));
				qos.add(q);

				q = new ServiceQos();
				q.setName(EntityConstants.QOS_RETRY);
				q.setValue(retry.getValue(retry.getSelectedIndex()));
				qos.add(q);

				context.put(ServiceConstants.SERVICE_QOS, qos);

				listener.onContentChanged(
						ServiceContentConstants.CREATE_SERVICE_CONFIG,
						ServiceContentConstants.CREATE_SERVICE_BUS_BINDING, null);

			}
		});
		hp.add(p);
		hp.add(new HTML("&nbsp;"));
		hp.add(b);

		panel.add(table);

		panel.add(hp);

		//panel.add(b);

	}

}
